package net.hn.hnms.biz.safety.trinity.group.service.impl;

import net.hn.hnms.biz.safety.trinity.group.domain.dto.NameAndIntegerDto;
import net.hn.hnms.biz.safety.trinity.group.domain.dto.RiskListDto;
import net.hn.hnms.biz.safety.trinity.group.domain.vo.RiskLevelVO;
import net.hn.hnms.biz.safety.trinity.group.domain.vo.RiskListVo;
import net.hn.hnms.biz.safety.trinity.group.domain.vo.RiskManagementVo;
import net.hn.hnms.biz.safety.trinity.group.domain.vo.RisklevelSumByCompanyVo;
import net.hn.hnms.biz.safety.trinity.group.mapper.RiskControlListMapper;
import net.hn.hnms.biz.safety.trinity.group.mapper.RiskControlMeasureMapper;
import net.hn.hnms.biz.safety.trinity.group.service.RiskRecordGroupService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.redis.domain.SysOrganization;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
//集团端风险实现类
public class RiskRecordGroupServiceImpl implements RiskRecordGroupService {

    /**
     * 风险表Mapper
     */
    @Resource
    private RiskControlListMapper riskControlListMapper;

    /**
     * 风险措施表
     */
    @Resource
    RiskControlMeasureMapper riskControlMeasureMapper;

    /**
     * 风险列表分页查询
     * @param pageQuery
     * @param riskListDto
     * @return
     */
    @Override
    public ResponsePage<RiskListVo> riskListPageList(PageQuery pageQuery, RiskListDto riskListDto) {
        if (pageQuery.getPageNum() == null) {
            pageQuery.setPageNum(0);
        }
        if (pageQuery.getPageSize() == null) {
            pageQuery.setPageSize(10);
        }
        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? pageQuery.getPageNum() * pageQuery.getPageSize():0);
        //分页查询风险列表
        List<RiskListVo> riskListVoList = riskControlListMapper.selectListPage(pageQuery,riskListDto);
        Integer total = 0;
        if (!riskListVoList.isEmpty()) {
            RiskListVo riskListVo = riskListVoList.get(0);
            if (riskListVoList != null) {
                 total = riskListVo.getTotal();
            }
            for (RiskListVo listVo : riskListVoList) {
                //煤矿编码
                String mineCode = listVo.getMineCode();
                if (StringUtils.isNotBlank(mineCode)) {
                    SysOrganization sysOrganization = RedisUtils.getCacheObject(Constants.SYS_CODE_KEY + mineCode);
                    if (sysOrganization != null) {
                        listVo.setMineName(sysOrganization.getOrgName());
                    }
                }

                //公司编码
                String companyCode = listVo.getCompanyCode();
                if (StringUtils.isNotBlank(companyCode)) {
                    SysOrganization sysOrganization = RedisUtils.getCacheObject(Constants.SYS_CODE_KEY + companyCode);
                    if (sysOrganization != null) {
                        listVo.setCompanyName(sysOrganization.getOrgName());
                    }
                }
            }

           return new ResponsePage<>(riskListVoList, total);
        }
        return new ResponsePage<>(new ArrayList<>(), total);
    }

    /**
     * 按风险等级统计风险数量-饼图
     * @param companyCode
     * @param mineCode
     * @return
     */
    @Override
    public RiskLevelVO riskCountByRiskLevel(String companyCode, String mineCode) {
        RiskLevelVO riskLevelVO = new RiskLevelVO(0,0,0,0);
        List<NameAndIntegerDto> dtoList=riskControlListMapper.riskCountByRiskLevel(companyCode, mineCode);

        for (NameAndIntegerDto dto : dtoList) {
            if ("重大风险（一级）".equals(dto.getName())) {
                riskLevelVO.setHeavyRiskCount(dto.getValue());
            } else if ("较大风险（二级）".equals(dto.getName())) {
                riskLevelVO.setMajorRiskCount(dto.getValue());
            } else if ("一般风险（三级）".equals(dto.getName())) {
                riskLevelVO.setGeneralRiskCount(dto.getValue());
            } else if ("低风险（四级）".equals(dto.getName())) {
                riskLevelVO.setLowRiskCount(dto.getValue());
            }
        }
        return riskLevelVO;
    }

    /**
     * 按风险等级统计风险数量-柱状图
     * @param companyCode
     * @return
     */
    @Override
    public List<RisklevelSumByCompanyVo> riskCountByRiskLevelByCompanyCode(String companyCode) {
        List<RisklevelSumByCompanyVo> voList = new ArrayList<>();
        if (StringUtils.isNotBlank(companyCode)) {
            //查询当前公司下面的所有煤矿
            //获取当前公司信息
            SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);
            if (sysOrganizationVo == null) {
                throw new HuaNengException(Constants.FAIL, "无法获取当前公司的子煤矿信息，请检查词典" + companyCode);
            }
            //遍历当前公司下面的所有煤矿
            List<SysOrganizationVo> subCodeList = sysOrganizationVo.getSubCodeList();
            for (SysOrganizationVo organizationVo : subCodeList) {
                RisklevelSumByCompanyVo risklevelSumByCompanyVo = new RisklevelSumByCompanyVo();
                String orgName = organizationVo.getOrgName();
                String orgCode = organizationVo.getOrgCode();
                RiskLevelVO riskLevelVO = this.riskCountByRiskLevel(null, orgCode);
                risklevelSumByCompanyVo.setCode(orgCode);
                risklevelSumByCompanyVo.setName(orgName);
                risklevelSumByCompanyVo.setRiskLevelVO(riskLevelVO);
                voList.add(risklevelSumByCompanyVo);
            }
        } else {
            //查询所有公司
            //查询所有公司
            SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + 0);
            if (sysOrganizationVo == null) {
                throw new HuaNengException(Constants.FAIL, "无法获取所有公司信息，请检查词典" + companyCode);
            }
            //遍历当前公司下面的所有煤矿
            List<SysOrganizationVo> subCodeList = sysOrganizationVo.getSubCodeList();
            for (SysOrganizationVo organizationVo : subCodeList) {
                RisklevelSumByCompanyVo risklevelSumByCompanyVo = new RisklevelSumByCompanyVo();
                String orgName = organizationVo.getOrgName();
                String orgCode = organizationVo.getOrgCode();
                RiskLevelVO riskLevelVO = this.riskCountByRiskLevel(orgCode, null);
                risklevelSumByCompanyVo.setCode(orgCode);
                risklevelSumByCompanyVo.setName(orgName);
                risklevelSumByCompanyVo.setRiskLevelVO(riskLevelVO);
                voList.add(risklevelSumByCompanyVo);
            }
        }
        return voList;
    }

    /**
     *
     * @param companyCode
     * @param mineCode
     * @return
     */
    @Override
    public RiskManagementVo riskManagement(String companyCode, String mineCode) {
        RiskManagementVo vo = new RiskManagementVo(0,0,0,0,0,0);
        //查询风险总数
        Integer riskCount = riskControlListMapper.selectRiskAllCount(companyCode, mineCode);
        vo.setRiskCount(riskCount);

        //查询管控措施总数
        Integer riskControlMeasureCount=riskControlMeasureMapper.selectRiskControlMeasureMapper(companyCode, mineCode);
        vo.setControlMeasureCount(riskControlMeasureCount);

        //查询重大风险
        RiskLevelVO riskLevelVO = riskCountByRiskLevel(companyCode, mineCode);
        Integer heavyRiskCount = riskLevelVO.getHeavyRiskCount();
        vo.setHeavyRiskCount(heavyRiskCount);

        //年度辨识
        Integer annualIdentificationCount =riskControlListMapper.selectRiskIdentifyTypeCount(companyCode, mineCode,"年度风险辨识");
        vo.setAnnualIdentificationCount(annualIdentificationCount);
        //专项辨识
        Integer specialIdentificationCount=riskControlListMapper.selectRiskIdentifyTypeCount(companyCode, mineCode, "专项风险辨识");
        vo.setSpecialIdentificationCount(specialIdentificationCount);

        //其他辨识
        vo.setOtherIdentificationCount(riskCount - annualIdentificationCount - specialIdentificationCount);

        return vo;
    }
}
